﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Squog_1._1.Menu;
using Squog_1._1;

namespace Squog_1._1.Managers
{
    public class WindowManager : DrawableGameComponent
    {
        private static Stack<Window> viewStack;
        private static Window CurrentWindow;
        public static int WindowWidth { get; private set; }
        public static int WindowHeight { get; private set; }
        private static ContentManager Content;

        public WindowManager(Game game)
            : base(game)
        {
            viewStack = new Stack<Window>();
            Content = game.Content;
        }

        protected override void LoadContent()
        {
            WindowResized();
            ChangeWindows(new StartWindow());
            ButtonLayout.LoadTextures(Content);
            base.LoadContent();
        }

        public static void WindowResized()
        {
            //TODO: Adjust to use the players Settings
            WindowWidth = Game1.graphics.GraphicsDevice.PresentationParameters.BackBufferWidth;
            WindowHeight = Game1.graphics.GraphicsDevice.PresentationParameters.BackBufferHeight;
        }

        #region Update/Draw
        public override void Update(GameTime gameTime)
        {
            CurrentWindow.Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (Window w in viewStack.Reverse<Window>())
            {
                w.SpriteBatchBegin();
                w.Draw(gameTime);
                w.SpriteBatchEnd();
            }

            base.Draw(gameTime);
        }
        #endregion

        #region HelperMethods
        public static void ChangeWindows(Window NewWindow)
        {
            CurrentWindow = NewWindow;
            CurrentWindow.LoadContent(Content);

            viewStack.Clear();
            viewStack.Push(CurrentWindow);
        }

        public static void AddWindow(Window NewWindow)
        {
            CurrentWindow = NewWindow;
            CurrentWindow.LoadContent(Content);

            viewStack.Push(CurrentWindow);
        }

        public static void RemoveWindow()
        {
            viewStack.Pop();
            CurrentWindow = viewStack.First<Window>();
        }

        public static SpriteBatch GenerateNewSpriteBatch()
        {
            return new SpriteBatch(Game1.graphics.GraphicsDevice);
        }
        #endregion
    }
}
